<?php
/**
 * Created by PhpStorm.
 * User: QIUXINSHU
 * Date: 2016/8/17
 * Time: 20:26
 */

namespace Group\Controller;


use OT\TagLib\Think;
use Think\Controller;
use Common\Model\OptionModel;
use Think\Page;

class DetailController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!session('userdata')) {
            header('location:' . U('Group/Login/index'));
            exit;
        }
    }

    /*
     * 佣金分配详情
     * */
    public function commission_detail()
    {
        $id = I('get.id');
        $func=I('get.func');
        //开始时间
        $start = I('get.start_time');
        $start = strtotime($start);
        if (empty($start)) {
            $start = strtotime(date('Y-m'));
        }
        $this->assign('start', $start);

        //结束时间
        $end = I('get.end_time');
        $end = strtotime($end);
        if (empty($end)) {
            $end = time();
        }
        $this->assign('end', $end);
        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $mobile = I('get.mobile');
        if (empty($mobile)) {
            if (empty($id)) {
                $user = session('userdata');
                $memberarr = unserialize($user['g_member']);
                //获取所有组成员下的用户
                foreach ($memberarr as $val) {
                    $level['_string'] = "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$')";
                    $level['mobile']=array('neq',"");
                    $follow = M('follow')->field('id')->where($level)->select();
                    foreach ($follow as $value) {
                        $inarr[] = $value['id'];
                    }
                }
                $inarr = array_unique(array_merge($memberarr, $inarr));
                $where['commission_user_id'] = array('in', $inarr);
            } else {
                if ($func=='only') {
                    $where['commission_user_id'] = $id;
                } else {
                    $level['_string'] = "(route like '{$id}') or (route regexp ',{$id},') or (route regexp '^{$id},') or (route regexp ',{$id}$')";
                    $follow = M('follow')->field('id')->where($level)->select();
                    foreach ($follow as $value) {
                        $inarr[] = $value['id'];
                    }
                    $where['commission_user_id'] = array('in', $inarr);
                }

            }

        }else {
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if(in_array($searchdata['id'],$memberarr) or $pan){
                $where['commission_user_id'] = $searchdata['id'];
            }else{
                $this->error('对不起,没有该成员');
            }


        }

        $type = I('get.type');
        if ($type == 'wei') {
            $where['a.status'] = 0;
        } else {
            $where['a.status'] = 1;
        }

        $where['create_time'] = array(array('egt', $start), array('elt', $end+86400));
        $count = M('futures_commission')->alias('a')->where($where)->count();
        $page = new \Think\Page($count, 20);

        $showdata = M('futures_commission')->alias('a')->field('a.commission,a.note,a.status,a.create_time,b.id,b.nickname')->where($where)->join('left join `wp_follow` b on a.commission_user_id=b.id')->order('a.create_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('page', $page);
        $this->assign('showdata', $showdata);
        $this->display();
    }

    /*
     * 提现，充值详情
     * */
    public function access()
    {
        $func=I('get.func');
        $type = I('type');
        $id = I('get.id');
        //开始时间
        $start = I('get.start_time');
        $start = strtotime($start);
        if (empty($start)) {
            $start = strtotime(date('Y-m'));
        }
        $this->assign('start', $start);

        //结束时间
        $end = I('get.end_time');
        $end = strtotime($end);
        if (empty($end)) {
            $end = time();
        }
        $this->assign('end', $end);
        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $mobile = I('get.mobile');
        if (empty($mobile)) {
            if (empty($id)) {
                $user = session('userdata');
                $memberarr = unserialize($user['g_member']);
                foreach ($memberarr as $val) {
                    $level['_string'] = "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$')";
                    $level['mobile']=array('neq',"");
                    $follow = M('follow')->field('id')->where($level)->select();
                    foreach ($follow as $value) {
                        $inarr[] = $value['id'];
                    }
                }
            } else {
                if ($func=='only') {
                    $inarr[] = $id;
                } else {
                    $level['_string'] = "(route like '{$id}') or (route regexp ',{$id},') or (route regexp '^{$id},') or (route regexp ',{$id}$')";
                    $follow = M('follow')->field('id')->where($level)->select();
                    foreach ($follow as $value) {
                        $inarr[] = $value['id'];
                    }
                }

            }

        } else {
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if(in_array($searchdata['id'],$memberarr) or $pan){
                $inarr=array($searchdata['id']);
            }else{
                $this->error('对不起,没有该成员');
            }
        }
        //获取所有组成员下的用户

        $where['user_id'] = array('in', $inarr);
        $where['is_pay'] = 1;

        if ($type == 'recharge') {
            $where['create_time'] = array(array('egt', $start), array('elt', $end+86400));
            $count = M('futures_recharge')->where($where)->count();
            $page = new \Think\Page($count, 20);
            $showdata = M('futures_recharge')->alias('a')->field('a.money,a.create_time,b.id,b.nickname,b.mobile,b.money as banlance')->where($where)->join('left join `wp_follow` b on a.user_id=b.id')->order('create_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        } else {
            $where['pay_time'] = array(array('egt', $start), array('elt', $end+86400));
            $count = M('futures_withdrawals')->where($where)->count();
            $page = new \Think\Page($count, 20);
            $showdata = M('futures_withdrawals')->alias('a')->field('a.money,a.pay_time,b.id,b.nickname,b.mobile,b.money as banlance')->where($where)->join('left join `wp_follow` b on a.user_id=b.id')->order('pay_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        }
        $this->assign('page', $page);
        $this->assign('showdata', $showdata);
        $this->display();
    }

    public function profit()
    {
        $func=I('get.func');
        $type = I('type');
        $id = I('get.id');
        //开始时间
        $start = I('get.start_time');
        $start = strtotime($start);
        if (empty($start)) {
            $start = strtotime(date('Y-m'));
        }
        $this->assign('start', $start);

        //结束时间
        $end = I('get.end_time');
        $end = strtotime($end);
        if (empty($end)) {
            $end = time();
        }
        $this->assign('end', $end);
        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $mobile = I('get.mobile');
        if (empty($mobile)) {
            if (empty($id)) {
                $user = session('userdata');
                $memberarr = unserialize($user['g_member']);
                //获取所有组成员下的用户
                foreach ($memberarr as $val) {
                    $level['_string'] = "(route like '{$val}') or (route regexp ',{$val},') or (route regexp '^{$val},') or (route regexp ',{$val}$')";
                    $level['mobile']=array('neq',"");
                    $follow = M('follow')->field('id')->where($level)->select();
                    foreach ($follow as $value) {
                        $inarr[] = $value['id'];
                    }
                }
            } else {
                if ($func=='only') {
                    $inarr[] = $id;
                } else {
                    $level['_string'] = "(route like '{$id}') or (route regexp ',{$id},') or (route regexp '^{$id},') or (route regexp ',{$id}$')";
                    $follow = M('follow')->field('id')->where($level)->select();
                    if(empty($follow)){
                        $inarr[]=$id;
                    }else{
                        foreach ($follow as $value) {
                            $inarr[] = $value['id'];
                        }
                    }

                }

            }

        } else {
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if(in_array($searchdata['id'],$memberarr) or $pan){
                $inarr= array($searchdata['id']);
            }else{
                $this->error('对不起,没有该成员');
            }
        }


        if ($type == "henzhi") {
            $henmodel = new OptionModel('order');
            $wherehen['user_id'] = array('in', $inarr);
            $wherehen['status'] = 2;
            $wherehen['start_time']=array('$gte'=>$start,'$lte'=>$end+86400);
            $count=$henmodel->where($wherehen)->count();
            $page=new Page($count,20);
            $showdata = $henmodel->where($wherehen)->limit($page->firstRow.','.$page->listRows)->order('start_time desc')->select();
            foreach ($showdata as $key => $val) {
                $userdata = M('follow')->field('mobile,nickname,money')->find($val['user_id']);
                $showdata[$key]['mobile'] = $userdata['mobile'];
                $showdata[$key]['nickname'] = $userdata['nickname'];
                $showdata[$key]['money'] = $userdata['money'];
            }
        } elseif ($type == "euro") {
            $euromodel = new OptionModel('euro_order');
            $wherehen['user_id'] = array('in', $inarr);
            $wherehen['start_time']=array('$gte'=>$start,'$lte'=>$end+86400);
            $wherehen['status'] = 2;
            $count=$euromodel->where($wherehen)->count();
            $page=new Page($count,20);
            $showdata = $euromodel->where($wherehen)->limit($page->firstRow.','.$page->listRows)->order('start_time desc')->select();
            foreach ($showdata as $key => $val) {
                $userdata = M('follow')->field('mobile,nickname,money')->find($val['user_id']);
                $showdata[$key]['mobile'] = $userdata['mobile'];
                $showdata[$key]['nickname'] = $userdata['nickname'];
                $showdata[$key]['money'] = $userdata['money'];
            }
        } elseif ($type == "gold") {
            $goldmodel = new OptionModel('goldorder');
            $wherehen['user_id'] = array('in', $inarr);
            $wherehen['status'] = 2;
            $wherehen['start_time']=array('$gte'=>$start,'$lte'=>$end+86400);
            $count=$goldmodel->where($wherehen)->count();
            $page=new Page($count,20);
            $showdata = $goldmodel->where($wherehen)->limit($page->firstRow.','.$page->listRows)->order('start_time desc')->select();
            foreach ($showdata as $key => $val) {
                $userdata = M('follow')->field('mobile,nickname,money')->find($val['user_id']);
                $showdata[$key]['mobile'] = $userdata['mobile'];
                $showdata[$key]['nickname'] = $userdata['nickname'];
                $showdata[$key]['money'] = $userdata['money'];
            }
        }elseif($type=="dollar"){
            $dollarmodel = new OptionModel('dollarorder');
            $wherehen['user_id'] = array('in', $inarr);
            $wherehen['status'] = 2;
            $wherehen['start_time']=array('$gte'=>$start,'$lte'=>$end+86400);
            $count=$dollarmodel->where($wherehen)->count();
            $page=new Page($count,20);
            $showdata = $dollarmodel->where($wherehen)->limit($page->firstRow.','.$page->listRows)->order('start_time desc')->select();
            foreach ($showdata as $key => $val) {
                $userdata = M('follow')->field('mobile,nickname,money')->find($val['user_id']);
                $showdata[$key]['mobile'] = $userdata['mobile'];
                $showdata[$key]['nickname'] = $userdata['nickname'];
                $showdata[$key]['money'] = $userdata['money'];
            }
        }
        $this->assign('page',$page);
        $this->assign('showdata', $showdata);
        $this->display();
    }
}